/**
 * @Author: oldTea
 * @CreateTime: 2024-11-29
 * @Description:
 * @Version: 1.0
 */
package com.ambition.demo.juc;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 *@Author: oldTea
 *@CreateTime: 2024-11-29
 *@Description:
 *@Version: 1.0
 */
public class CompletableFutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() ->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
            System.out.println("I'll run in a separate thread than the main thread.");
        });
        completableFuture.get();

        CompletableFuture<String> completableFuture1 = CompletableFuture.supplyAsync(() ->{
            try {
                TimeUnit.SECONDS.sleep(1);




                
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
           return "任务完成";
        });
        String s = completableFuture1.get();
        System.out.println(s);
    }
}


